package gryd.boardcheckertest;

import static org.junit.Assert.*;

import gryd.board.BadBoardException;
import gryd.board.BoardChecker;

import java.io.IOException;
import java.io.InputStreamReader;

import org.junit.Test;

public class BoardCheckerTest {
	@Test
	public void testErrorBoards() {
		for (int boardnumber = 1; boardnumber < 12; boardnumber++) {
			try {
				testBoard("gryd/bugmaps/bugmap" + boardnumber + ".txt");
				System.out.println("fail at " + boardnumber);
				fail();
			} catch (BadBoardException e) {
				System.out.println("bugmap"+boardnumber+": caught error: " + e);
			}
		}
	}

	public void testBoard(String name) throws BadBoardException {
		InputStreamReader i = new InputStreamReader(ClassLoader.getSystemResourceAsStream(name));
		StringBuffer sb = new StringBuffer("");
		try {
			int c;
			while ((c = i.read()) != -1) {
				sb.append((char) c);
			}
		} catch (IOException e) {
			System.out.println(e);
			fail();
		}
		BoardChecker.checkBoard(sb.toString());
	}

	@Test
	public void testCorrectBoards() {
		try {
			testBoard("gryd/bugmaps/board2-nobugs.txt");
		} catch (BadBoardException e) {
			System.out.println(e + " at board2-nobugs");
			fail();
		}
		try{
			testBoard("gryd/bugmaps/board1");
		} catch (BadBoardException e) {
			System.out.println(e + " at board1");
			fail();
		}
		try{
			testBoard("gryd/bugmaps/board2");
		} catch (BadBoardException e) {
			System.out.println(e + " at board2");
			fail();
		}
		try{
			testBoard("gryd/bugmaps/board3");
		} catch (BadBoardException e) {
			System.out.println(e + " at board3");
			fail();
		}
	}
}
